<?php
class MY_Utilities {
    public function __construct() {
    }
    function unicode_str_filter($str) {
        $unicode = array(
            'a' => 'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
            'd' => 'đ',
            'e' => 'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
            'i' => 'í|ì|ỉ|ĩ|ị',
            'o' => 'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
            'u' => 'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
            'y' => 'ý|ỳ|ỷ|ỹ|ỵ',
            'A' => 'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'D' => 'Đ',
            'E' => 'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'I' => 'Í|Ì|Ỉ|Ĩ|Ị',
            'O' => 'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'U' => 'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'Y' => 'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
        );
        foreach ($unicode as $nonUnicode => $uni) {
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
        }
        return $str;
    }
    public function seo_string_to_url($str) {
        $str = $this->unicode_str_filter($str);
        $str = urlencode($str);
        //TODO
        $str = preg_replace('/\+/', '-', $str); 
        return $str;
    }
    
    public function get_id_from_uri($uri) {
        preg_match('/\-(\d+)$/', $uri, $mach);
        $id = isset($mach[1])?$mach[1]:NULL;
        return $id;
    }
    // TODO: By quoc;
    public function set_url_segment($array_segments){
            $index=0;
            $url_segment='';
                    foreach ($array_segments as $key=>$val){
                        $index++;
                       if($index<count($array_segments))
                            $url_segment.=$key."/".$val.'/';
                       if($index==count($array_segments))
                            $url_segment.=$key;
                    }
                    return $url_segment;
        }
}
